import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Label;


public class Bullet extends MapObject{
	static int bulletSize = 5;
	int damage;
	Robot shooter;
	Robot damagedRobot = null;
	
	public double deltaX;
	public double deltaY;
	
	public Bullet(Robot shooter, double deltaX, double deltaY, int damage){
		this.x = shooter.x;
		this.y = shooter.y;
		this.speed = -1;
		this.angle = -1;
		this.shooter = shooter;
		this.deltaX = deltaX;
		this.deltaY = deltaY;
		this.damage = damage;
		
		avatar = new Label(Map.getInstance().canvas, SWT.NONE);
		avatar.setBounds(getX(), getY(), bulletSize, bulletSize);
		
		Image avatarImage = new Image(avatar.getDisplay(), Map.defaultImageFilename);
		avatar.setImage(avatarImage);
		
		avatar.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDown(MouseEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("Bullet shooted by "+Bullet.this.shooter.name+"and hited "+damagedRobot+"  - "+Map.getInstance().bullets.indexOf(this));
				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	@Override
	public void drawAvatar() {
		if(avatar != null && !avatar.isDisposed()){
			avatar.setBounds(getX(), getY(), bulletSize, bulletSize);
		}
	}

	@Override
	public void play() {
		x += deltaX;
		y += deltaY;
		if(getX()<0 || getY()<0 || getX()>Map.getInstance().width || getY()>Map.getInstance().height){
			autoDestroy();
		}
		drawAvatar();
	}
	
	public void autoDestroy(){
		hit(null);
	}
	
	public int getDamagePower(){
		return damage;
	}
	
	public void hit(Robot damagedRobot){
		this.damagedRobot = damagedRobot;
		if(damagedRobot!=null){
			shooter.credit += 10+getDamagePower();
			damagedRobot.damage(getDamagePower());
			if(damagedRobot.life<=0){
				shooter.credit += 20;
				shooter.killedRobots++;
				damagedRobot.checkLife();
			}
		}
		hideAvatar();
	}
}
